package com.danicsoft.daide.command;

import java.util.ArrayList;
import java.util.List;

import com.danicsoft.daide.token.StandardToken;
import com.danicsoft.daide.token.TokenMap;
import com.danicsoft.daide.token.TokenGroup;


/**
 * A unit position that contains an optional list of retreat options, if this is
 * a retreat phase and the unit has been dislodged.
 * 
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 * @see UnitPos
 */
public class RetreatableUnitPos extends UnitPos {


	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(super.toString());
		if(isRetreating) {
			sb.append(" retreating to ");
			for(StandardToken tok : retreatOptions) {
				sb.append(tok.toString());
				sb.append(' ');
			}
		}
		return sb.toString();
	}

	/**
	 * A list of provinces it is possible to retreat to.
	 */
	private List<StandardToken> retreatOptions = new ArrayList<StandardToken>();

	@Override
	public void doIO(TokenIO tokenIO, TokenGroup tokens) throws ParseException {
		TokenGroup sGroup = tokenIO.subGroup(tokens);
		super.handleSubGroup(tokenIO, sGroup);
		isRetreating = tokenIO.doConditional(sGroup, TokenMap.getParameterToken(TokenMap.Parameters.MRT), isRetreating);
		if (!isRetreating)
			return;

		retreatOptions = tokenIO.doTokenList(tokenIO.subGroup(sGroup), retreatOptions, TokenMap.provinces);

	}

	@Override
	public IOTarget newInstance() {
		return new RetreatableUnitPos();
	}


}